//引入大仓库
import pinia from '@/store'
import useUserStore from '@/store/modules/user';
let userStore = useUserStore(pinia)
//路由鉴权
//引入路由器
import router from '@/router';
//引入进度条
import Nprogress from 'nprogress';
//引入进度条的样式
import "nprogress/nprogress.css"
// import useUserStore from './store/modules/user';

//进度条的加载小圆圈不要
Nprogress.configure({ showSpinner: false })

//存储用户未登录可以访问路由的路径
let whiteList = ['/home', '/hospital/register', '/hospital/detail', '/hospital/notice', '/hospital/close', '/hospital/search'];

//添加相应的全局守卫
//前置守卫
router.beforeEach((to, from, next) => {
    //访问路由组件的之前，进度条开始动
    Nprogress.start()
    //动态设置网页左上角的标题
    document.title = `尚医通-${to.meta.title}`
    // console.log(to);

    //判断用户是否登录-token
    let token = userStore.userInfo.token;
    //用户登录了
    if (token) {
        next();
    } else {
        //用户未登录 如果路径在这个数组中存在
        if (whiteList.includes(to.path)) {
            next();
        } else {
            //登录组件显示
            userStore.visiable = true;
            next({ path: '/home', query: { redirect: to.fullPath } })
        }

    }
})

//后置守卫
router.afterEach((to, from) => {
    //访问路由组件成功，进度条结束
    Nprogress.done()
})